Skip to content

feat(cloudflare): Propagate traceparent to RPC calls - via fetch#19991

Open
JPeer264 wants to merge 3 commits intodevelopfrom
jp/rpc-instrument
Open

feat(cloudflare): Propagate traceparent to RPC calls - via fetch#19991
JPeer264 wants to merge 3 commits intodevelopfrom
jp/rpc-instrument

Conversation

@JPeer264
Copy link
Copy Markdown
Member

@JPeer264 JPeer264 commented Mar 26, 2026

relates to #19327

related to #16898 (it is not really closing it as we just add context propagation without adding spans for individual calls. It needs to be defined if we need it)

It is important to know that these RPC calls do only work with the .fetch call:

const id = env.MY_DURABLE_OBJECT.idFromName('workflow-test');
const stub = env.MY_DURABLE_OBJECT.get(id);

await stub.fetch(new Request('http://my-worker/my-do-call'));

This adds RPC fetch calls between:

This works by instrumenting env (via instrumentEnv), which then goes over the bindings and see if there is a DurableObject or a normal Fetcher (full list of current bindings: https://developers.cloudflare.com/workers/runtime-apis/bindings/). This got inspired by how otel-cf-workers instruments their env: https://github.com/evanderkoogh/otel-cf-workers/blob/effeb549f0a4ed1c55ea0c4f0d8e8e37e5494fb3/src/instrumentation/env.ts

With this PR I added a lot of tests to check if trace propagation works (so this PR might look like it added a lot of LoC, but it is mostly tests). So I added it for schedule and queue, but it is not possible for email and tail with wrangler dev.

Potential things to change

Trace propagagtion

I added the addTraceHeaders.ts helper, as there is currently no way to reuse the existing logic (it is baked-in into the fetch instrumentations). It would be nice once #19960 lands that we can reuse it in Cloudflare to reuse existing code. I tried to write couple of tests so we don't have duplicated headers.

Adding extra spans

So there is actually a guide by OTel to add RPC spans, but was talking with someone from the OTel maintainers and they meant that this wouldn't be necessary as we already have an http.server span from out instrumented DurableObjects (and other resources) - so it wouldn't add much of information.

Without RPC span:

Screenshot 2026-03-25 at 10 59 01

With RPC span:

Screenshot 2026-03-25 at 10 55 48

@JPeer264 JPeer264 requested review from Lms24, logaretm and timfish March 26, 2026 14:38
@JPeer264 JPeer264 self-assigned this Mar 26, 2026
@linear-code
Copy link
Copy Markdown

linear-code bot commented Mar 26, 2026

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 26, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Cloudflare

  • Propagate traceparent to RPC calls - via fetch by JPeer264 in #19991
  • Support basic WorkerEntrypoint by JPeer264 in #19884

Core

  • Export a reusable function to add tracing headers by JPeer264 in #20076
  • Support registerTool/registerResource/registerPrompt in MCP integration by betegon in #20071
  • Support embeddings in langchain by nicohrubec in #20017

Deps

  • Bump @hapi/content from 6.0.0 to 6.0.1 by dependabot in #20102
  • Bump bundler plugins to 5.2.0 by chargome in #20122
  • Bump lodash.template from 4.5.0 to 4.18.1 by dependabot in #20085
  • Bump @xmldom/xmldom from 0.8.3 to 0.8.12 by dependabot in #20066

Other

  • (aws-serverless) Add lambda extension to npm package by andreiborza in #20133
  • (core, node) Portable Express integration by isaacs in #19928
  • (deno) Add denoRuntimeMetricsIntegration by chargome in #20023
  • (node, bun) Enforce minimum collection interval in runtime metrics integrations by chargome in #20068
  • (nuxt) Exclude tracing meta tags on cached pages in Nuxt 5 by s1gr1d in #20168
  • (react-router) Export sentryOnError by chargome in #20120
  • Span Streaming by Lms24 in #19119

Bug Fixes 🐛

Core

  • Only attach flags context to error events by Lms24 in #20116
  • Replace regex with string check in stack parser to prevent main thread blocking by chargome in #20089
  • Set span.status to error when MCP tool returns JSON-RPC error response by betegon in #20082

Other

  • (angular) Bump TypeScript to ~6.0.0 in angular-21 E2E test app by andreiborza in #20134
  • (aws-serverless) Add timeout to _endSpan forceFlush to prevent Lambda hanging by logaretm in #20064
  • (cloudflare) Ensure every request instruments functions by JPeer264 in #20044
  • (gatsby) Fix errorHandler signature to match bundler-plugin-core API by JPeer264 in #20048

Internal Changes 🔧

Core

  • Do not emit spans for chats.create in google-genai by nicohrubec in #19990
  • Unify .do* span ops to gen_ai.generate_content by nicohrubec in #20074
  • Simplify addResponseAttributes in openai integration by nicohrubec in #20013
  • Extract shared endStreamSpan for AI integrations by nicohrubec in #20021
  • Remove provider-specific AI span attributes in favor of gen_ai attributes in sentry conventions by nicohrubec in #20011

Deps

  • Bump mshick/add-pr-comment from dd126dd8c253650d181ad9538d8b4fa218fc31e8 to e7516d74559b5514092f5b096ed29a629a1237c6 by dependabot in #20078
  • Bump getsentry/craft/.github/workflows/changelog-preview.yml from 2.24.1 to 2.25.2 by dependabot in #20081

Other

  • (deps-dev) Bump effect from 3.20.0 to 3.21.0 by dependabot in #19999
  • (node) Add node integration tests for Vercel ToolLoopAgent by nicohrubec in #20087
  • (nuxt) Make Nuxt 5 (nightly) E2E optional by s1gr1d in #20113
  • (oxlint) Add typeawareness into oxlintrc by JPeer264 in #20075
  • Update validate-pr workflow by stephanie-anderson in #20072
  • Remove unused tsconfig-template folder by mydea in #20067

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 26, 2026

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 25.72 kB +0.31% +77 B 🔺
@sentry/browser - with treeshaking flags 24.21 kB +0.34% +82 B 🔺
@sentry/browser (incl. Tracing) 42.73 kB +1.36% +573 B 🔺
@sentry/browser (incl. Tracing, Profiling) 47.35 kB +1.25% +583 B 🔺
@sentry/browser (incl. Tracing, Replay) 81.54 kB +0.75% +600 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 71.11 kB +0.78% +549 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 86.25 kB +0.7% +594 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 98.45 kB +0.56% +541 B 🔺
@sentry/browser (incl. Feedback) 42.51 kB +0.21% +87 B 🔺
@sentry/browser (incl. sendFeedback) 30.39 kB +0.27% +80 B 🔺
@sentry/browser (incl. FeedbackAsync) 35.38 kB +0.24% +83 B 🔺
@sentry/browser (incl. Metrics) 27.04 kB +0.31% +83 B 🔺
@sentry/browser (incl. Logs) 27.18 kB +0.3% +81 B 🔺
@sentry/browser (incl. Metrics & Logs) 27.86 kB +0.29% +80 B 🔺
@sentry/react 27.48 kB +0.26% +71 B 🔺
@sentry/react (incl. Tracing) 45.05 kB +1.28% +566 B 🔺
@sentry/vue 30.56 kB +1.61% +483 B 🔺
@sentry/vue (incl. Tracing) 44.59 kB +1.23% +538 B 🔺
@sentry/svelte 25.74 kB +0.3% +76 B 🔺
CDN Bundle 28.41 kB +0.3% +83 B 🔺
CDN Bundle (incl. Tracing) 43.75 kB +1.46% +628 B 🔺
CDN Bundle (incl. Logs, Metrics) 29.78 kB +0.31% +92 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 44.83 kB +1.5% +660 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 68.59 kB +0.16% +103 B 🔺
CDN Bundle (incl. Tracing, Replay) 80.64 kB +0.78% +621 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 81.66 kB +0.75% +604 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 86.17 kB +0.72% +615 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 87.2 kB +0.71% +610 B 🔺
CDN Bundle - uncompressed 82.99 kB +0.34% +279 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 129.77 kB +1.5% +1.92 kB 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 87.14 kB +0.33% +279 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 133.19 kB +1.46% +1.92 kB 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 210.12 kB +0.14% +279 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 246.65 kB +0.79% +1.92 kB 🔺
⛔️ CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed (max: 250 kB) 250.05 kB +0.78% +1.92 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 259.56 kB +0.75% +1.92 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 262.95 kB +0.74% +1.92 kB 🔺
@sentry/nextjs (client) 47.47 kB +1.24% +579 B 🔺
@sentry/sveltekit (client) 43.2 kB +1.36% +577 B 🔺
@sentry/node-core 57.86 kB +3.78% +2.1 kB 🔺
@sentry/node 174.8 kB +1.4% +2.4 kB 🔺
@sentry/node - without tracing 97.97 kB +2.02% +1.94 kB 🔺
@sentry/aws-serverless 115.22 kB +2.11% +2.38 kB 🔺

View base workflow run

Copy link
Copy Markdown
Collaborator

@timfish timfish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@JPeer264 JPeer264 changed the title feat(cloudflare): Propagate traceparent to RPC calls feat(cloudflare): Propagate traceparent to RPC calls - via fetch Mar 31, 2026
@JPeer264 JPeer264 force-pushed the jp/rpc-instrument branch from 71a3936 to 3097f54 Compare March 31, 2026 11:25
@Lms24
Copy link
Copy Markdown
Member

Lms24 commented Mar 31, 2026

It would be nice once #19960 lands that we can reuse it in Cloudflare to reuse existing code. I tried to write couple of tests so we don't have duplicated headers.

FYI, #19960 landed and I agree! Right now, our various request instrumentations combine the "logic"/"algorithm" code with platform specific code how to set headers. I think it would be good if we could strip out the algorithm part into a common helper, and only leave the platform-specific parts to the individual instrumentations. From my PoV a good improvement but something we can also tackle at a later point. Happy to leave this up to you :)

Comment on lines +13 to +15
const newInit = addTraceHeaders(input, init);

return fetchFn(input, newInit);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m/q: is this how we add our tracing headers in our core/browser fetch instrumentation as well? Just wanted to double check because injecting headers into fetch arguments is a bit tricky with the various forms of input and init and the precedence.

I think adding headers to init is fine. Though us deep-copying headers from input to init, and then init again might be unexpected if one of the objects is reused by users across multiple requests. Anyway, not saying something is off but that we should double check that this is how we do it in the other SDKs as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, that would be indeed a bad behavior - I'll change the behavior to match the others

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lms24 the headers are now attached with the same function as the other SDKs: _INTERNAL_getTracingHeadersForFetchRequest.

@JPeer264 JPeer264 force-pushed the jp/rpc-instrument branch from 3097f54 to fb6a835 Compare April 1, 2026 12:34
@JPeer264
Copy link
Copy Markdown
Member Author

JPeer264 commented Apr 1, 2026

@Lms24 I just reworked the way how tracing headers are generated. I actually exported getTracingHeadersForFetchRequest from core, which is used inside instrumentFetchRequests in core. I actually saw that mergeBaggageHeaders is not applied there, but I think this should be added (maybe in a follow up PR?!).

I can also outsource getTracingHeadersForFetchRequest into its own PR if wanted, to make this PR a little bit more focused on one thing. (Edit: I actually outsourced this: #20076)

@JPeer264 JPeer264 requested a review from Lms24 April 1, 2026 15:19
@JPeer264 JPeer264 force-pushed the jp/rpc-instrument branch from 77c7260 to ef05c42 Compare April 1, 2026 15:20
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 9,137 - 8,855 +3%
GET With Sentry 1,706 19% 1,740 -2%
GET With Sentry (error only) 5,939 65% 6,020 -1%
POST Baseline 1,190 - 1,163 +2%
POST With Sentry 595 50% 588 +1%
POST With Sentry (error only) 1,039 87% 1,035 +0%
MYSQL Baseline 3,185 - 3,155 +1%
MYSQL With Sentry 398 12% 440 -10%
MYSQL With Sentry (error only) 2,611 82% 2,567 +2%

View base workflow run

JPeer264 added a commit that referenced this pull request Apr 7, 2026
part of #15942 

This is a basic instrumentation for `WorkerEntrypoint`. There is no
extra `instrumentWorkerEntrypoint` exported, and should work directly
with `withSentry` OOTB:

```js
class MyWorker extends WorkerEntrypoint {}

export default Sentry.withSentry(() => ({}), MyWorker)
```

To support the full `WorkerEntrypoint` there is the need to instrument
[RPC
methods](#16898).
Without that we can't fully support `WorkerEntrypoint`s but just the
basics of it. This can be added once #19991 lands (it is safe to review
now and instrument the env in a separate PR)

--- 

If you look at the instrumentations themselves, there is no
`isInstrumented` or `markAsInstrumented`. The reason is that on the one
hand this is not required, as every request is spawning a new instance
of the `WorkerEntrypoint` and on the other hand it wouldn't work as it
would mark it as instrumented, but on the request the `fetch` is
overridden and not instrumented.
JPeer264 added a commit that referenced this pull request Apr 9, 2026
This PR is an extraction of #19991 

It basically exports `getTracingHeadersForFetchRequest`, which was
previously only exported for testing, but offers a great functionality
if you want to add tracing headers to a request. I renamed it as
`addTracingHeadersToFetchRequest` sounded a little misleading, as it
didn't really add headers to the request, as it returned the extracted
headers from the request (or init, if there are any).

### Open question

I added `@hidden` and `@internal` to it, not sure if this is an approach
we follow. I'm ok to remove it from the jsdoc
@JPeer264 JPeer264 force-pushed the jp/rpc-instrument branch from 291bbd3 to bc033a9 Compare April 9, 2026 18:33
@JPeer264 JPeer264 force-pushed the jp/rpc-instrument branch from bc033a9 to f62418d Compare April 9, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants